package com.buba.system.controller;

import com.buba.system.pojo.SystemUsers;
import com.buba.system.service.RoleService;
import com.buba.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @description:
 * @Author: 游王子
 * @date: 2021/8/19 11:57
 */
@RestController
@RequestMapping("/UserController")
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @Autowired
    @Qualifier("RoleServiceImpl")
    private RoleService roleService;

    @RequestMapping("/login")
    public Map<String, Object> login(SystemUsers systemUsers) {
        Map<String, Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        try {
            if (!subject.isAuthenticated()) {
                // 创建帐号密码令牌
                UsernamePasswordToken token = new UsernamePasswordToken(systemUsers.getUserName(), systemUsers.getUserPassword());
                // 通过subject.login()进行登录认证
                subject.login(token);
            }
            map.put("message", "登录成功");
        } catch (UnknownAccountException uae) {
            map.put("message", "帐号不存在");
        } catch (IncorrectCredentialsException ice) {
            map.put("message", "密码错误");
        } catch (LockedAccountException lae) {
            // 该用户名的帐户被锁定，无法登录。给他们一个信息?
        }
        return map;
    }

    /**
     * 退出清空主体
     * @param response
     * @throws IOException
     */
    @RequestMapping("/logout")
    public void testRole(HttpServletResponse response) throws IOException {
        //登出清除缓存
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        response.sendRedirect("login.html");
    }

    @RequestMapping("register")
    public Integer register(SystemUsers systemUsers){
        systemUsers.setUserID(System.currentTimeMillis()+"");
        //利用SimpleHash来设置md5(上面三种都可以通过这个来设置，这里举例加盐加散列次数的)
        //第一个参数是算法名称，这里指定md5，第二个是要加密的密码，第三个参数是加盐，第四个是散列次数
        SimpleHash hash = new SimpleHash("md5", systemUsers.getUserPassword(), systemUsers.getUserName(), 2);
        systemUsers.setUserPassword(hash.toString());
        Integer count=userService.register(systemUsers);
        return count;
    }
    //查询是否已有该账号
    @RequestMapping("registerSel")
    public Integer registerSel(SystemUsers systemUsers){
        SystemUsers count=userService.login(systemUsers);
        if(count==null){
            return 0;
        }
        return 1;
    }
}
